/*
 * Copyright (C) 2011 xjuraj
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.websearch;

import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.enums.searchserviceenum.SearchServiceEnum;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;
import sk.lieskove.jianghongtiao.websearch.service.SearchQuery;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;

import java.util.List;

/**
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
public class ThreadFullWebSearch extends Thread {
    
    private transient Logger log = Logger.getLogger(ThreadFullWebSearch.class);
    private transient PropertiesUtils pu = new PropertiesUtils(ThreadFullWebSearch.class);

    private transient final FullWebSearch webSearch = new FullWebSearch();
    private transient SearchQuery query = null;
    private transient Integer numberOfResults = null;
    private transient SearchServiceEnum[] services = null;
    List<WebSearchResponse> results = null;

    public ThreadFullWebSearch(String name) {
        super(name);
    }
    
    @Override
    public void run() {
        if((query != null) && (numberOfResults != null) && (services != null)){
            results = webSearch.search(query, numberOfResults, services);
        } else if((query != null) && (numberOfResults != null)){
            results = webSearch.search(query, numberOfResults);
        } else if((query != null) && (services != null)){
            results = webSearch.search(query, services);
        } else if((query != null)){
            results = webSearch.search(query);
        } else {
            throw new IllegalStateException("Bad combination of parameters. "
                    + "You have to call search function at first.");
        }
    }

    public void searchAll(SearchQuery query) {
        this.query = query;
        this.numberOfResults = null;
        this.services = null;
        this.results = null;
    }

    public void searchAll(SearchQuery query, int numberOfResults) {
        this.query = query;
        this.numberOfResults = numberOfResults;
        this.services = null;
        this.results = null;
    }

    public void search(SearchQuery query, SearchServiceEnum... services) {
        this.query = query;
        this.numberOfResults = null;
        this.services = services;
        this.results = null;
    }

    public void search(SearchQuery query, int numberOfResults, SearchServiceEnum... services) {
        this.query = query;
        this.numberOfResults = numberOfResults;
        this.services = services;
        this.results = null;
    }
    
    public List<WebSearchResponse> getResults(){
        return results;
    }
    
}
